import sys

from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from unionlotto import Ui_MainWindow


class TabDemo(QMainWindow):

    def slot_small_tab(self):
        print("slot_small_tab_index", self.tab_widget.currentIndex(), "===", self.tab_widget.count())
        if self.tab_widget.count() - 1 == self.tab_widget.currentIndex():
            self.addTab()
            self.tab_widget.setCurrentIndex(self.tab_widget.count() - 2)
            print("add")

        else:
            self.selectIndex = self.tab_widget.currentIndex()

    def addTab(self):
        tabView = Ui_MainWindow()
        self.tab_widget.insertTab(self.tab_widget.count() - 1, tabView, "選項卡")
        # self.tab_widget.insertTab(self.tab_widget.count()-1, tabView, "選項卡 "+str(self.tab_widget.count()))

    def tabClose(self, index):

        self.tab_widget.setCurrentIndex(index - 1)
        print("currentindex", index, "count", self.tab_widget.count())
        if self.tab_widget.count() > 2:
            self.tab_widget.tabBar().removeTab(index)

    def initWindowHead(self, win):
        label_head_title = QLabel()
        label_head_title.setText('小林通 - 基础窗口')

        button_head_min = QPushButton()
        button_head_min.setObjectName('button_head_min')
        # button_head_min.clicked.connect(self.onWindowMin)

        button_head_max = QPushButton()
        button_head_max.setObjectName('button_head_max')
        # button_head_max.clicked.connect(self.onWindowMax)

        button_head_close = QPushButton()
        button_head_close.setObjectName('button_head_close')
        # button_head_close.clicked.connect(self.onWindowClose)

        layout = QHBoxLayout(win)
        layout.addWidget(label_head_title)
        layout.addStretch(0)
        layout.addWidget(button_head_min)
        layout.addWidget(button_head_max)
        layout.addWidget(button_head_close)
        layout.setSpacing(9)
        layout.setContentsMargins(18, 0, 18, 0)

    def __init__(self, parent=None):
        super(TabDemo, self).__init__(parent)
        self.resize(1200, 720)
        self.setWindowTitle("奇門血刃偏財巫眼翟軒權")  # 设置窗口标题
        # self.setWindowFlag(Qt.FramelessWindowHint)

        self.selectIndex = 0

        self.tab_widget = QtWidgets.QTabWidget()

        self.tab_widget.setTabsClosable(True)
        # hlayout = QHBoxLayout()
        # hlayout.addWidget(self.tab_widget)
        # hlayout.setContentsMargins(6, 6, 6, 6)
        # self.setLayout(hlayout)

        self.setCentralWidget(self.tab_widget)

        # 创建3个选项卡小控件窗口
        self.tab1 = QWidget()
        # self.tab2 = QWidget()
        # self.tab3 = QWidget()
        # self.tab_widget.tabBar().setMovable(True)
        # self.tab_widget.tabBar().move(0, 100)
        self.tab_widget.setTabsClosable(True)
        self.tab_widget.tabCloseRequested.connect(self.tabClose)
        # self.tab_widget.setMovable(True)
        self.tab_widget.currentChanged.connect(self.slot_small_tab)

        # 将三个选项卡添加到顶层窗口中
        # self.tab_widget.addTab(self.tab1, "Tab 1")
        # self.tab_widget.addTab(self.tab2, "Tab 2")
        # self.tab_widget.addTab(self.tab3, "Tab 3")

        # self.tab_reportManner = QtWidgets.QWidget()
        # self.tab_reportManner.setObjectName("tab_reportManner")
        # icon = QIcon()
        # icon.addPixmap(QtGui.QPixmap("./icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        # self.addTab(self.tab_reportManner, icon, "tab2")
        fakeButton = QPushButton("+")
        self.tab_widget.addTab(fakeButton, "+")

        self.right = self.tab_widget.tabBar().RightSide
        self.tab_widget.tabBar().setTabButton(self.tab_widget.count() - 1, self.right, None)

        # self.tab_widget.tabBar().setUpdatesEnabled(False)
        # fakeButton.move(self.tab_widget.tabBar().width() - fakeButton.width(), 0)
        # self.tab_widget.tabBar().setUpdatesEnabled(True)

        # 每个选项卡自定义的内容
        self.tab1UI()
        # self.tab2UI()
        # self.tab3UI()
        self.win_head = QWidget()
        self.win_head.setObjectName('win_head')
        self.win_head.setMouseTracking(True)
        self.win_head.setFixedHeight(36)
        self.initWindowHead(self.win_head)

    def tab1UI(self):
        # 表单布局
        layout = QFormLayout()
        # 添加姓名，地址的单行文本输入框
        layout.addRow('姓名', QLineEdit())
        layout.addRow('地址', QLineEdit())
        # 设置选项卡的小标题与布局方式
        # self.tab_widget.setTabText(0, 'tab2')
        self.tab1.setLayout(layout)

    def tab2UI(self):
        # zhu表单布局，次水平布局
        layout = QFormLayout()
        sex = QHBoxLayout()

        # 水平布局添加单选按钮
        sex.addWidget(QRadioButton('男'))
        sex.addWidget(QRadioButton('女'))

        # 表单布局添加控件
        layout.addRow(QLabel('性别'), sex)
        layout.addRow('生日', QLineEdit())

        # 设置标题与布局
        self.tab_widget.setTabText(1, 'tab2')
        self.tab2.setLayout(layout)

    def tab3UI(self):
        # 水平布局
        layout = QHBoxLayout()

        # 添加控件到布局中
        layout.addWidget(QLabel('科目'))
        layout.addWidget(QCheckBox('物理'))
        layout.addWidget(QCheckBox('高数'))

        # 设置小标题与布局方式
        self.tab_widget.setTabText(2, 'tab2')
        self.tab3.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = TabDemo()
    demo.show()
    sys.exit(app.exec_())
